package com.stenio.fancat.common;

import com.stenio.fancat.util.StringUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author stenio
 */
public class PageHelperInterceptor implements HandlerInterceptor {

    public static final String PAGE_NUM = "pageNum";
    public static final String PAGE_SIZE = "pageSize";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (handler instanceof HandlerMethod) {
            String pageNum = request.getParameter(PAGE_NUM);
            String pageSize = request.getParameter(PAGE_SIZE);
            if (StringUtil.isNumber(pageNum) && StringUtil.isNumber(pageSize)) {
//                PageHelper.initPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            }
            if (request.getParameter("orderBy") != null) {
//                PageHelper.setOrderBy(request.getParameter("orderBy"));
            }
        }
        return true;
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        PageHelper.clearPage();
    }

}
